#include "GlLoader.h"
#include "Log.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
namespace GL
{

    unsigned char *GlLoader::LoadImageByUrl(std::string ImageUrl, ImageData &data)
    {
        int source;
        stbi_set_flip_vertically_on_load(true);
        unsigned char *image = stbi_load(ImageUrl.c_str(), &data.Width, &data.Height, &source, 0);
        return image;
    }
    void GlLoader::free(unsigned char *data)
    {
        stbi_image_free(data);
    }
    ShaderSourceData GlLoader::LoadShaderSource(const std::string &FilePath)
    {
        std::ifstream stream(FilePath);
        std::string line;
        std::stringstream ss[2];
        ShaderSourceType type = ShaderSourceType::NONE;
        while (getline(stream, line))
        {

            if (line.find("#Shader") != std::string::npos)
            {
                // 开始读取到Shader
                if (line.find("Vertex") != std::string::npos)
                {
                    // 读取顶点Shader
                    type = ShaderSourceType::VERTEX;
                }
                else if (line.find("Fragment") != std::string::npos)
                {
                    // 读取片段Shader
                    type = ShaderSourceType::FRAGMENT;
                }
            }
            else
            {
                ss[(int)type] << line << "\n";
            }
        }
        return {ss[0].str(), ss[1].str()};
    }
}